/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package personal.dao;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import personal.obj.User;
import personal.utility.DBUtility;

/**
 *
 * @author PHATNBSE61057
 */
public class UserDAO implements Serializable {

    private final DBUtility utility = new DBUtility();

    public UserDAO() {
    }

    public User getUserByUsername(String usernameS) {
        Connection con = null;
        PreparedStatement pst = null;
        ResultSet rs = null;
        try {
            con = utility.makeConnection();
            String query = "SELECT * FROM [User] WHERE Username = ?";
            pst = con.prepareStatement(query);
            pst.setString(1, usernameS);
            rs = pst.executeQuery();
            if (rs.next()) {
                String username = rs.getString("Username");
                String password = rs.getString("Password");
                String email = rs.getString("Email");
                String fullname = rs.getString("Fullname");
                String phone = rs.getString("Phone");
                boolean isActive = rs.getBoolean("IsActive");
                int roleID = rs.getInt("RoleID");
                User user = new User(username, password, email, fullname, phone,
                        isActive, roleID);
                return user;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (pst != null) {
                    pst.close();
                }
                if (con != null) {
                    con.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public boolean updateUserInfo(User user) {
        Connection con = null;
        PreparedStatement pst = null;
        int rowChanged;
        try {
            con = utility.makeConnection();
            String query = "UPDATE [User] SET Email = ?, "
                    + "Fullname = ?, Phone = ?, IsActive = ?, RoleID = ? "
                    + "WHERE Username = ?";
            pst = con.prepareStatement(query);
            pst.setString(1, user.getEmail());
            pst.setString(2, user.getFullname());
            pst.setString(3, user.getPhone());
            pst.setBoolean(4, user.isIsActive());
            pst.setInt(5, user.getRoleID());
            pst.setString(6, user.getUsername());
            rowChanged = pst.executeUpdate();
            if (rowChanged > 0) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (pst != null) {
                    pst.close();
                }
                if (con != null) {
                    con.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    public boolean updateUserPassword(String username, String password) {
        Connection con = null;
        PreparedStatement pst = null;
        int rowChanged;
        try {
            con = utility.makeConnection();
            String query = "UPDATE [User] SET [Password] = ? WHERE Username = ?";
            pst = con.prepareStatement(query);
            pst.setString(1, password);
            pst.setString(2, username);
            rowChanged = pst.executeUpdate();
            if (rowChanged > 0) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (pst != null) {
                    pst.close();
                }
                if (con != null) {
                    con.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    public User checkLogin(String usernameS, String passwordS) {
        Connection con = null;
        PreparedStatement pst = null;
        ResultSet rs = null;
        try {
            con = utility.makeConnection();
            String query = "SELECT * FROM [User] WHERE Username = ? AND Password = ?";
            pst = con.prepareStatement(query);
            pst.setString(1, usernameS);
            pst.setString(2, passwordS);
            rs = pst.executeQuery();
            if (rs.next()) {
                String username = rs.getString("Username");
                String password = rs.getString("Password");
                String email = rs.getString("Email");
                String fullname = rs.getString("Fullname");
                String phone = rs.getString("Phone");
                boolean isActive = rs.getBoolean("IsActive");
                int roleID = rs.getInt("RoleID");
                User user = new User(username, password, email, fullname, phone,
                        isActive, roleID);
                return user;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (pst != null) {
                    pst.close();
                }
                if (con != null) {
                    con.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public boolean registerUser(User user) {
        Connection con = null;
        PreparedStatement pst = null;
        int rowChanged;
        try {
            con = utility.makeConnection();
            String query = "INSERT INTO [User] (Username,Password,Email,Fullname,Phone,IsActive,RoleID) "
                    + "VALUES (?,?,?,?,?,?,?)";
            pst = con.prepareStatement(query);
            pst.setString(1, user.getUsername());
            pst.setString(2, user.getPassword());
            pst.setString(3, user.getEmail());
            pst.setNString(4, user.getFullname());
            pst.setString(5, user.getPhone());
            pst.setBoolean(6, user.isIsActive());
            pst.setInt(7, user.getRoleID());
            rowChanged = pst.executeUpdate();
            if (rowChanged > 0) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (pst != null) {
                    pst.close();
                }
                if (con != null) {
                    con.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return false;
    }
}
